package com.approval.approvalapp.index.cygn.wzly;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.WzlyListBean;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.ToastUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WzlyActivity extends BaseActivity implements LrvAdapter.ClickItemLrvListener, RrvAdapter.RrvClickListener {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.lrv)
    RecyclerView lrv;
    @Bind(R.id.rrv)
    RecyclerView rrv;
    @Bind(R.id.hj)
    TextView hj;
    @Bind(R.id.numtv)
    TextView numtv;
    @Bind(R.id.next_tv)
    TextView nextTv;
    private List<WzlyListBean> wzlyListBeans;
    private List<WzlyListBean.ListBean> littlelist;
    private LrvAdapter lrvAdapter;
    private RrvAdapter rrvAdapter;
    public static  WzlyActivity instance;

    @Override
    public int getLayoutId() {

        instance = this;
        return R.layout.activity_wzly;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        instance = null;
    }

    @Override
    public void initView() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("region_code", SpUtil.getString(this, SpUtil.REGION_CODE, ""));
        Log.d("WzlyActivityss", jsonObject.toString());
        Log.d("WzlyActivityss", SpUtil.getString(this, SpUtil.USER_RUN_ID, ""));
        Log.d("WzlyActivityss", SpUtil.getString(this, SpUtil.F_userid, ""));
        NetRequsetUtil.getInstance().netRequestPostJson("v1/Center/material_add", jsonObject.toString(), new NetDownResponse() {
            @Override
            public void success(String str) {
                wzlyListBeans = JSON.parseArray(str, WzlyListBean.class);
                littlelist = wzlyListBeans.get(0).getList();
                wzlyListBeans.get(0).setSelect(true);

                lrv.setLayoutManager(new LinearLayoutManager(WzlyActivity.this));
                lrvAdapter = new LrvAdapter();
                lrvAdapter.setWzlyListBeanList(wzlyListBeans, WzlyActivity.this);
                lrv.setAdapter(lrvAdapter);


                rrv.setLayoutManager(new LinearLayoutManager(WzlyActivity.this));
                rrvAdapter = new RrvAdapter();
                rrvAdapter.setLittlelist(littlelist, WzlyActivity.this);
                rrv.setAdapter(rrvAdapter);


            }

            @Override
            public void errowithresponse(String str) {

            }

            @Override
            public void erro() {

            }

            @Override
            public void finish() {

            }
        });

    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.back_iv, R.id.next_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.next_tv:
                Intent intent = new Intent();
                intent.setClass(this, WzlyQdActivity.class);
                List<WzlyListBean> list1 = new ArrayList<>();
                for (int i = 0; i < wzlyListBeans.size(); i++) {
                    if (wzlyListBeans.get(i).getRedcount() > 0) {
                        list1.add(wzlyListBeans.get(i));
                    }
                }

                if (list1.size() == 0){
                    return;
                }

                //过滤一下二维数组

                Iterator<WzlyListBean> iterator = list1.iterator();
                while (iterator.hasNext()){
                    WzlyListBean wzlyListBean = iterator.next();
                    Iterator<WzlyListBean.ListBean> iterator1 = wzlyListBean.getList().iterator();
                    while (iterator1.hasNext()){
                        WzlyListBean.ListBean bean = iterator1.next();
                        if (bean.getCurrent_count() == 0){
                            iterator1.remove();
                        }
                    }
                }

                intent.putExtra("list", (Serializable) list1);
                startActivity(intent);
                break;
        }
    }

    int currentLeftPosition;

    @Override
    public void clickItem(int position) {
        currentLeftPosition = position;
        for (int i = 0; i < wzlyListBeans.size(); i++) {
            if (i == position) {
                wzlyListBeans.get(i).setSelect(true);
                lrvAdapter.setWzlyListBeanList(wzlyListBeans, this);
                rrvAdapter.setLittlelist(wzlyListBeans.get(i).getList(), this);
            } else {
                wzlyListBeans.get(i).setSelect(false);
                lrvAdapter.setWzlyListBeanList(wzlyListBeans, this);
            }
        }
    }

    @Override
    public void add(int index) {
        int currentnum = wzlyListBeans.get(currentLeftPosition).getList().get(index).getCurrent_count() + 1;
        if (currentnum > Integer.valueOf(wzlyListBeans.get(currentLeftPosition).getList().get(index).getCount())) {
            ToastUtil.showToast(this, "最大为:  " + wzlyListBeans.get(currentLeftPosition).getList().get(index).getCount());
            return;
        }
        wzlyListBeans.get(currentLeftPosition).getList().get(index).setCurrent_count(currentnum);
        rrvAdapter.setLittlelist(wzlyListBeans.get(currentLeftPosition).getList(), this);

        int redcount = 0;
        for (int i = 0; i <
                wzlyListBeans.get(currentLeftPosition).getList().size(); i++) {
            redcount = redcount +
                    wzlyListBeans.get(currentLeftPosition).getList().get(i).getCurrent_count();
        }
        wzlyListBeans.get(currentLeftPosition).setRedcount(redcount);
        lrvAdapter.setWzlyListBeanList(wzlyListBeans, this);

        int allredcount = 0;
        for (int i = 0; i < wzlyListBeans.size(); i++) {
            allredcount = allredcount + wzlyListBeans.get(i).getRedcount();
        }
        numtv.setText(allredcount + "");

    }

    @Override
    public void subtract(int index) {
        int currentnum = wzlyListBeans.get(currentLeftPosition).getList().get(index).getCurrent_count() - 1;
        if (currentnum < 0) {
            return;
        }
        wzlyListBeans.get(currentLeftPosition).getList().get(index).setCurrent_count(currentnum);
        rrvAdapter.setLittlelist(wzlyListBeans.get(currentLeftPosition).getList(), this);

        int redcount = 0;
        for (int i = 0; i <
                wzlyListBeans.get(currentLeftPosition).getList().size(); i++) {
            redcount = redcount +
                    wzlyListBeans.get(currentLeftPosition).getList().get(i).getCurrent_count();
        }
        wzlyListBeans.get(currentLeftPosition).setRedcount(redcount);
        lrvAdapter.setWzlyListBeanList(wzlyListBeans, this);

        int allredcount = 0;
        for (int i = 0; i < wzlyListBeans.size(); i++) {
            allredcount = allredcount + wzlyListBeans.get(i).getRedcount();
        }
        numtv.setText(allredcount + "");
    }

    public void refresh(String id) {
        for (int i = 0; i < wzlyListBeans.size() ; i++) {
            wzlyListBeans.get(i).setSelect(false);
        }
        for (int i = 0; i < wzlyListBeans.size(); i++) {
            if (wzlyListBeans.get(i).getId() .equals(id)){
                wzlyListBeans.get(i).setSelect(true);
            }
        }

        lrvAdapter.notifyDataSetChanged();
    }
}
